package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MedicalRecord;
import com.ruoyi.system.domain.Register;
import com.ruoyi.system.service.IMedicalRecordService;
import com.ruoyi.system.service.impl.RegisterServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 病历记录Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "病历记录模块")
@RestController
@RequestMapping("/system/MedicalRecord")
public class MedicalRecordController extends BaseController {
    @Autowired
    private IMedicalRecordService medicalRecordService;

    /**
     * 查询病历记录列表
     */
    @ApiOperation(value = "查询病历记录列表")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:list')")
    @GetMapping("/list")
    public Page<MedicalRecord> list(MedicalRecord entity, Page<MedicalRecord> page) {

        LambdaQueryWrapper<MedicalRecord> lambdaQueryWrapper = new LambdaQueryWrapper<MedicalRecord>();
        return medicalRecordService.page(page, lambdaQueryWrapper);
    }

    /**
     * 导出病历记录列表
     */
    @ApiOperation(value = "导出病历记录列表")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:export')")
    @Log(title = "病历记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicalRecord medicalRecord) {
        LambdaQueryWrapper<MedicalRecord> queryWrapper = new LambdaQueryWrapper<>();
        List<MedicalRecord> list = medicalRecordService.list(queryWrapper);
        ExcelUtil<MedicalRecord> util = new ExcelUtil<MedicalRecord>(MedicalRecord.class);
        util.exportExcel(response, list, "病历记录数据");
    }


    /**
     * 获取病历记录详细信息
     */
    @ApiOperation(value = "获取病历记录详细信息")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(medicalRecordService.getById(id));
    }

    /**
     * 新增病历记录
     */
    @ApiOperation(value = "新增病历记录")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:add')")
    @Log(title = "病历记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicalRecord medicalRecord) {
        return toAjax(medicalRecordService.save(medicalRecord));
    }

    /**
     * 修改病历记录
     */
    @ApiOperation(value = "修改病历记录")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:edit')")
    @Log(title = "病历记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicalRecord medicalRecord) {
        medicalRecord.getCaseState();
        RegisterServiceImpl registerService=new RegisterServiceImpl();
        Register register=registerService.getById(medicalRecord.getRegisterId());
        register.setVisitState(medicalRecord.getCaseState());
        registerService.updateById(register);
        return toAjax(medicalRecordService.updateById(medicalRecord));
    }

    /**
     * 删除病历记录
     */
    @ApiOperation(value = "删除病历记录")
    @PreAuthorize("@ss.hasPermi('system:MedicalRecord:remove')")
    @Log(title = "病历记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(medicalRecordService.removeBatchByIds(Arrays.asList(Ids)));
    }

    /**
     * 根据挂号id查询
     */
    @GetMapping("/getByRegisterId")
    public MedicalRecord getByRegisterId(@RequestParam("RegisterId") Long RegisterId) {
        QueryWrapper<MedicalRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("RegisterId", RegisterId);
        MedicalRecord res = medicalRecordService.getOne(queryWrapper);
        if (res == null) {
            res = new MedicalRecord();
        }
        return res;
    }

    /**
     * 根据病历号查询
     */
    @GetMapping("/getByCaseNumber")
    public List<MedicalRecord> getByCaseNumber(@RequestParam("CaseNumber") Long CaseNumber) {
        QueryWrapper<MedicalRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CaseNumber", CaseNumber);
        List<MedicalRecord> res = medicalRecordService.list(queryWrapper);
        if (res.size() == 0) {
            res.add(new MedicalRecord());
        }
        return res;
    }
}
